package com.lync.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lync.controller.Code;
import com.lync.dao.ExamPaperDao;
import com.lync.domain.ExamPaper;
import com.lync.exception.BusinessException;
import com.lync.service.ExamPaperService;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.List;

@Slf4j
@Service
public class ExamPaperServiceImpl extends ServiceImpl<ExamPaperDao, ExamPaper> implements ExamPaperService {

    @Autowired
    private ExamPaperDao examPaperDao;

    @Override
    public boolean save(ExamPaper examPaper) {
        return examPaperDao.insert(examPaper) > 0;
    }

    @Override
    public boolean modify(ExamPaper examPaper) {
        return examPaperDao.updateById(examPaper) > 0;
    }

    @Override
    public boolean delete(Integer id) {
        return examPaperDao.deleteById(id) > 0;
    }

    @Override
    public ExamPaper getById(Integer id) {
        if (id<0){
            throw new BusinessException(Code.PROJECT_BUSINESS_ERROR,"请勿进行非法操作!");
        }
        return examPaperDao.selectById(id);
    }

    @Override
    public IPage<ExamPaper> getPage(int currentPage, int pageSize) {
        IPage page = new Page(currentPage,pageSize);
        examPaperDao.selectPage(page,null);
        return page;
    }

    @Override
    public IPage<ExamPaper> getPage(int currentPage, int pageSize, ExamPaper examPaper) {
        LambdaQueryWrapper<ExamPaper> lqw = new LambdaQueryWrapper<ExamPaper>();
        lqw.like(Strings.isNotEmpty(examPaper.getName()),ExamPaper::getName,examPaper.getName());

        IPage page = new Page(currentPage,pageSize);
        examPaperDao.selectPage(page,lqw);
        return page;
    }

    @Override
    public List<ExamPaper> getAll() {
        return null;
    }
}
